package com.szosen.jkzj.util;

import java.io.File;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Element;
import com.itextpdf.text.Font;
import com.itextpdf.text.Image;
import com.itextpdf.text.PageSize;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.Phrase;
import com.itextpdf.text.pdf.BaseFont;
import com.itextpdf.text.pdf.ColumnText;
import com.itextpdf.text.pdf.PdfPTable;
import com.itextpdf.text.pdf.PdfWriter;
import com.szosen.jkzj.activity.BaseActivity;
import com.szosen.util.FileSaveUtil;
import com.szosen.util.TimeHelper;
import com.szosen.util.Tools;

import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.widget.Toast;

public class T {
	private static Document document = null;

	public static void showLong(Context context, String text) {
		Toast.makeText(context, text, Toast.LENGTH_LONG).show();
	}

	public static void showShort(Context context, String text) {
		Toast.makeText(context, text, Toast.LENGTH_SHORT).show();
	}

	public static Font getChineseFont() {
		BaseFont bf = null;
		Font fontChinese = null;
		try {
			bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
					BaseFont.NOT_EMBEDDED);

			fontChinese = new Font(bf, 12, Font.NORMAL);
		} catch (DocumentException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return fontChinese;
	}

	// 添加标题
	public static void setTitle(Context context, String data) {
		try {
			open();
			BaseFont bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
					BaseFont.NOT_EMBEDDED);
			Font fontChinese = new Font(bf, 20, Font.NORMAL);
			Paragraph p = new Paragraph(data, fontChinese);
			p.setAlignment(Element.ALIGN_CENTER);
			p.setAlignment(Element.TITLE);
			document.add(p);
			document.add(new Paragraph("\n"));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 用户基本信息
	public static void setUserInfo(Context context) {
		try {
			open();
			Paragraph paragraph = new Paragraph();
			paragraph.setAlignment(Element.ALIGN_JUSTIFIED);
			paragraph.setFont(getChineseFont());
			paragraph.add("身份证:" + "440510195412022343");
			paragraph.add("   姓名:" + "王友人");
			paragraph.add("   性别:" + "男");
			paragraph.add("   出生日期:" + "1954年12月02日");
			paragraph.add("   时间:" + Tools.GetCurrentTime());
			paragraph.add(new Paragraph("\n"));
			paragraph.add("健康报告：");
			document.add(paragraph);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void newFile(Context context, String data)
			throws FileNotFoundException, DocumentException {
		// Step 1—Create a Document.
		open();

		// Step 2—Get a PdfWriter instance.

		// Step 3—Open the Document.

		// Step 4—Add content.
		document.add(new Paragraph(data, getChineseFont()));
		document.add(new Paragraph("\n\n"));

	}

	public static void newFileTitle(Context context)
			throws FileNotFoundException, DocumentException {
		open();
		document.add(new Paragraph("分析建议：", getChineseFont()));
		document.add(new Paragraph("\n"));
	}

	private static void open() throws DocumentException, FileNotFoundException {
		File mainfile = null;
		mainfile = new File(Environment.getExternalStorageDirectory(),
				"PerosonHealth");
		if (!mainfile.mkdir()) {
			try {
				mainfile.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		if (document == null) {
			document = new Document(PageSize.A4);
			PdfWriter.getInstance(document, new FileOutputStream(mainfile + "/"
					+ "刘友人" + "检测报告" + ".pdf"));
			document.open();
		}
	}

	public static void close() {
		if (document != null) {
			document.close();
			document = null;
		}

	}

	public static void addTable(String[] mode, String[] data, String n)
			throws FileNotFoundException, DocumentException {
		open();
		PdfPTable table = new PdfPTable(mode.length);
		for (int aw = 0; aw < mode.length; aw++) {
			Phrase ph = new Phrase(mode[aw], getChineseFont());
			table.addCell(ph);
		}
		document.add(table);
		PdfPTable table1 = new PdfPTable(data.length);
		for (int aw = 0; aw < data.length; aw++) {
			Phrase ph = new Phrase(data[aw], getChineseFont());
			table1.addCell(ph);
			// Cell cell = new Cell(ph);
		}
		document.add(table1);
		if (n != null) {
			document.add(new Paragraph(n));
		}
	}

	public static void aa() {
		try {
			open();
		} catch (FileNotFoundException e1) {
			e1.printStackTrace();
		} catch (DocumentException e1) {
			e1.printStackTrace();
		}
		PdfPTable table = new PdfPTable(5);

		table.addCell(new Paragraph("检测时间", getChineseFont()));
		table.addCell(new Paragraph("检测项目", getChineseFont()));
		table.addCell(new Paragraph("测量值", getChineseFont()));
		table.addCell(new Paragraph("参考值", getChineseFont()));
		table.addCell(new Paragraph("结论", getChineseFont()));

		try {
			document.add(table);
		} catch (DocumentException e) {
			e.printStackTrace();
		}
	}

	public static void test() {
		Document document = new Document(PageSize.A4.rotate(), 5, 5, 5, 5);

		try {
			File pdfFile = new File(FileSaveUtil.getSDCardPath(), "555.pdf");
			if (!pdfFile.exists()) {
				pdfFile.createNewFile();
			}
			FileOutputStream fos = new FileOutputStream(pdfFile);
			PdfWriter.getInstance(document, fos);
						document.open();
			BaseFont baseFontChinese = BaseFont.createFont(
					"STSong-Light", "UniGB-UCS2-H",
					BaseFont.NOT_EMBEDDED);
			Font fontChinese = new Font(baseFontChinese, 14,
					Font.NORMAL);
			Paragraph title = new Paragraph("心电诊断报告\n", new Font(
					baseFontChinese, 16, Font.NORMAL));
			Paragraph printTime = new Paragraph("打印时间："
					+ TimeHelper.getCurrentTime(), fontChinese);
			Paragraph doctor = new Paragraph(
					"医生签名：                       ", fontChinese);
			printTime.setAlignment(Element.ALIGN_RIGHT);
			doctor.setAlignment(Element.ALIGN_RIGHT);
			title.setAlignment(Element.ALIGN_CENTER);// 居中显示
			document.add(title);
			document.add(printTime);
			PdfPTable table = new PdfPTable(5);
			table.addCell(new Paragraph("检测时间", getChineseFont()));
			table.addCell(new Paragraph("检测项目", getChineseFont()));
			table.addCell(new Paragraph("测量值", getChineseFont()));
			table.addCell(new Paragraph("参考值", getChineseFont()));
			table.addCell(new Paragraph("结论", getChineseFont()));
			document.add(table);
			// step 4:
			document.add(new Paragraph("这个是什么东西啊", setChineseFont()));
			document.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 产生PDF字体
	public static Font setChineseFont() {
		BaseFont bf = null;
		Font fontChinese = null;
		try {
			bf = BaseFont.createFont("STSong-Light", "UniGB-UCS2-H",
					BaseFont.NOT_EMBEDDED);
			fontChinese = new Font(bf, 12, Font.NORMAL);
		} catch (DocumentException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return fontChinese;
	}

	public static void save(String[][] data, String time) {

		PdfPTable table = new PdfPTable(5);

		table.addCell(new Paragraph("检测时间", getChineseFont()));
		table.addCell(new Paragraph("检测项目", getChineseFont()));
		table.addCell(new Paragraph("测量值", getChineseFont()));
		table.addCell(new Paragraph("参考值", getChineseFont()));
		table.addCell(new Paragraph("结论", getChineseFont()));
		/*
		 * PdfPCell cell = new PdfPCell(new Phrase(time)); cell.setRowspan(11);
		 * table.addCell(cell);
		 */

		for (int i = 0; i < data.length; i++) {
			for (int j = 0; j < 5; j++) {
				Phrase ph = new Phrase(data[i][j], getChineseFont());
				table.addCell(ph);
			}
		}
		try {
			document.add(table);
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

}
